import apis from './indexapi.js'
// const BASE_URL = import.meta.env.DEV;
const BASE_URL = window.url;
//请求的实例
const api = apis();
// API调用封装
export  const  emapi = {		
    // 工单达成率
    getWorkOrderRate(productLine) {
        const params = {
            pathId: 222,
            parameter: JSON.stringify({
                deploymentName: "WorkOrderAchievementRate",
                requestMap: {
                    product_line: productLine
                }
            })
        }
    return api.hDoPost(`${BASE_URL}/api/Agency/GetDat`, null, params);
    },

    // 本月产率平面靶
    getPlanarTargetYield(productLine) {
        const params = {
            pathId: 222,
            parameter: JSON.stringify({
                deploymentName: "planarTargetYield",
                requestMap: {
                    product_line: productLine
                }
            })
        }
        return api.hDoPost(`${BASE_URL}/api/Agency/GetDat`, null, params)
    },

    // 本月产率旋转靶
    getRotatingTargetYield(productLine) {
        const params = {
            pathId: 222,
            parameter: JSON.stringify({
                deploymentName: "rotatingTargetYield2",
                requestMap: {
                    product_line: productLine
                }
            })
        }
        return api.hDoPost(`${BASE_URL}/api/Agency/GetDat`, null, params)
    },

    // 本月产率成分比例
    getComponentRatio(productLine) {
        const params = {
            pathId: 222,
            parameter: JSON.stringify({
                deploymentName: "componentRatioAndYield",
                requestMap: {
                    product_line: productLine
                }
            })
        }
        return api.hDoPost(`${BASE_URL}/api/Agency/GetDat`, null, params)
    },

    // 本月产率粉制备工序
    getPowderPreparation(productLine) {
        const params = {
            pathId: 222,
            parameter: JSON.stringify({
                deploymentName: "powderPreparationProcess",
                requestMap: {
                    product_line: productLine
                }
            })
        }
        return api.hDoPost(`${BASE_URL}/api/Agency/GetDat`, null, params)
    },

    // 本月产量统计
    getProductionStats(productLine) {
        const params = {
            pathId: 222,
            parameter: JSON.stringify({
                deploymentName: "productionOutputStatistics",
                requestMap: {
                    product_line: productLine
                }
            })
        }
        return api.hDoPost(`${BASE_URL}/api/Agency/GetDat`, null, params)
    },
}

// 使用示例
export const useProductionData = async (productLine = "产品线") => {
    try {
        const [
            workOrderRate,
            planarYield,
            rotatingYield,
            componentRatio,
            powderPrep,
            productionStats
        ] = await Promise.all([
            emapi.getWorkOrderRate(productLine),
            emapi.getPlanarTargetYield(productLine),
            emapi.getRotatingTargetYield(productLine),
            emapi.getComponentRatio(productLine),
            emapi.getPowderPreparation(productLine),
            emapi.getProductionStats(productLine)
        ])

        return {
            workOrderRate,
            planarYield,
            rotatingYield,
            componentRatio,
            powderPrep,
            productionStats
        }
    } catch (error) {
        console.error('获取生产数据失败:', error)
        return null
    }
}

// 错误处理工具
export const handleApiError = (error) => {
    if (error.response) {
        switch (error.response.status) {
            case 400:
                return '请求参数错误'
            case 500:
                return '服务器内部错误'
            default:
                return `请求失败: ${error.response.status}`
        }
    }
    return '网络错误'
} 